package cn.yanjr.mall.mall_base.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.stereotype.Repository;

import cn.yanjr.mall.mall_base.dao.SysDictDao;
import cn.yanjr.mall.mall_base.entity.SysDict;
import cn.yanjr.mall.mall_base.utils.JDBCUtils;
@Repository("sysDictDao")
public class SysDictDaoImpl implements SysDictDao{

	@Override
	public int save(SysDict dict) {
		// 添加用户的sql
		String sql = "insert into sys_dict values(null,?,?,?,?,?,?,?,?,?,?,0) ";
		// 用JDBCUtils工具创建一个queryRunner对象
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		int result = 0;
		try {
			// 用queryRunner保存用户信息
			result = queryRunner.update(sql, 
					dict.getLabel(),
					dict.getValue(),
					dict.getType(),
					dict.getDescription(),
					dict.getSort(),
					dict.getCreateBy(),
					dict.getCreateDate(),
					dict.getUpdateBy(),
					dict.getUpdateDate(),
					dict.getRemarks());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int update(SysDict dict) {
		// 更新用户的sql
		String sql = "update sys_dict set label=?,value=?,type=?,description=?,sort=?,createBy=?,createDate=?,updateBy=?,updateDate=?,remarks=? where id=?";
		// 用JDBCUtils工具创建一个queryRunner对象
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		int result = 0;
		try {
			// 用queryRunner保存用户信息
			result = queryRunner.update(sql, 
					dict.getLabel(),
					dict.getValue(),
					dict.getType(),
					dict.getDescription(),
					dict.getSort(),
					dict.getCreateBy(),
					dict.getCreateDate(),
					dict.getUpdateBy(),
					dict.getUpdateDate(),
					dict.getRemarks(),
					dict.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int delete(int dictId) {
		// 更新用户的sql
		String sql = "update sys_dict set delFlag=1 where id=?";
		// 用JDBCUtils工具创建一个queryRunner对象
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		int result = 0;
		try {
			// 用queryRunner保存用户信息
			result = queryRunner.update(sql, dictId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<SysDict> findAllList() {
		String sql = "select * from sys_dict where delFlag=? order by sort";
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		List<SysDict> list = null;
		try {
			list = queryRunner.query(sql, new BeanListHandler<SysDict>(SysDict.class), 0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<String> findTypeList() {
		String sql = "select type from sys_dict where delFlag=? group by type";
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		List<String> list = new ArrayList<String>();
		try {
			list = queryRunner.query(sql, new BeanListHandler<String>(String.class), 0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public SysDict findOne(int id) {
		String sql = "select * from sys_dict where id=?";
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		SysDict dict = null;
		try {
			dict = queryRunner.query(sql, 
					new BeanHandler<SysDict>(SysDict.class), 
					id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dict;
	}

	@Override
	public int count() {
		int count = 0;
		try {
			String sql = "SELECT COUNT(*) as count FROM sys_dict where delFlag=?";  
			QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
			count = queryRunner.query(sql, new ScalarHandler<Integer>(), 0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

}
